package org.scboy.dbms.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.SqlConfigDao;
import org.scboy.dbms.dao.SqlEntityDao;
import org.scboy.dbms.service.SqlConfigService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;

@Service
public class SqlConfigServiceImpl implements SqlConfigService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public Page page(Model params) {
		SqlConfigDao dao=factory.getDao(SqlConfigDao.class);
		params.orderBy("sqlId");
		return dao.page(params);
	}

	@Override
	@Transactional
	public boolean add(Model params) {
		SqlConfigDao dao=factory.getDao(SqlConfigDao.class);
		SqlEntityDao entityDao=factory.getDao(SqlEntityDao.class);
		
		params.add("id", UUID.randomUUID().toString());
		
		if(dao.count(Model.create().add("sqlId", params.getString("sqlId")).add("serverName", params.getString("serverName")))>0) {
			return false;
		}
		
		//插入sql配置明细表
		Model entity=Model.create();
		entity.add("id", UUID.randomUUID().toString());
		entity.add("configId", params.getString("id"));
		entity.add("entSql", params.getString("entSql"));
		entity.add("name", params.getString("name"));
		entity.add("entVersion", 0);
		entityDao.insert(entity);
		
		params.add("activeVersion", 0);
		
		int count= dao.insert(params);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean update(Model params) {
		SqlConfigDao dao=factory.getDao(SqlConfigDao.class);
		SqlEntityDao entityDao=factory.getDao(SqlEntityDao.class);
		
		//提升一个版本
		int activeVersion= params.getInt("activeVersion")+1;
		//插入sql配置明细表
		Model entity=Model.create();
		entity.add("id", UUID.randomUUID().toString());
		entity.add("configId", params.getString("id"));
		entity.add("entSql", params.getString("entSql"));
		entity.add("name", params.getString("name"));
		entity.add("entVersion",activeVersion);
		entityDao.insert(entity);
		
		params.add("activeVersion", activeVersion);
		
		int count= dao.update(params);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Model get(String id) {
		SqlConfigDao dao=factory.getDao(SqlConfigDao.class);
		SqlEntityDao entityDao=factory.getDao(SqlEntityDao.class);
		Model config= dao.get(Model.create().add("id", id));
		
		String entSql=entityDao.get(
				Model.create().add("configId", id)
				.add("entVersion", config.getString("activeVersion"))
				.addRc("entSql")
			).getString("entSql");
		config.add("entSql", entSql);
		return config;
	}

	@Override
	@Transactional
	public boolean delete(String id) {
		
		SqlConfigDao dao=factory.getDao(SqlConfigDao.class);
		
		SqlEntityDao entityDao=factory.getDao(SqlEntityDao.class);
		
		entityDao.delete(Model.create().add("configId", id));
		
		int count= dao.delete(Model.create().add("id",id));
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<Model> list(String appName) {
		SqlConfigDao dao=factory.getDao(SqlConfigDao.class);
		return dao.select(Model.create().add("serverName", appName));
	}

}
